<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * 统计券脚本
 */
class Cron_Statistics extends CI_Controller {
	private $ticketClass;
	private $cfdClass;
	private $logFile;
	private $admin_db;
	private $_db;
	public function __construct(){  
        parent::__construct();
        if(!is_cli()){
        	header("HTTP/1.0 404 Not Found");
        	show_error('Not Found',404);
        	die;
        }

        //加载操作类
		$this->load->library('TicketClass');
		$this->ticketClass = $this->ticketclass;
		$this->_db	= $this->load->database('default',true);
		$this->admin_db	= $this->load->database('admin',true);
		
		$this->load->config('cfd');
		$this->load->library('cfdClass');
		$this->cfdClass = $this->cfdclass;
    }

    /**
     * 统计CFD用户盈亏按小时更新
     * @return [type] [description]
     */
	public function userTicketYk(){
		$symbol	=	"DAOUSD";
		if(!$this->cfdClass->setProduct($symbol,'IB')){//设置产品
			die("无效操作！");
		}
		$productData = $this->cfdClass->getProductData();
		$settlementArr = explode(',',$productData['settlement']);
		$dst = $this->cfdClass->checkDST($symbol);
		$cur_H = date('H');
		$settlement_H	=	date('H',strtotime($settlementArr[$dst]));
		if($cur_H <= ($settlement_H+1)){
			$date	=	date("Y-m-d",strtotime("-1 days"));
			$this->load->library('Statistics');
			$dst	=	$this->statistics->getDst($date);
			if(empty($dst)){
				die("结算时间获取不到！");
			}
			$settlement_H	=	$dst['settlement_H'];
		}else{
			$date	=	date("Y-m-d");
		}

		$sdate		=	date("Y-m-d ".($settlement_H+1).":00:00",strtotime($date));
		$edate		=	date("Y-m-d ".$settlement_H.":59:59",strtotime($date)+24*3600);
			
		$where	=	"where kp='close' and date >='$sdate' and date <='$edate' and dtype='ticket' and yk>0";
		$sql	=	"select * from cfd_cfd_trade_log ".$where;
		echo $sql."\n";
		$query	=	$this->_db->query($sql);
		$result_array	=	$query->result_array();
		$user_list		=	array();
		if (!empty($result_array)) foreach($result_array as $items){
			$user_list[$items['uid']]['num']	=	isset($user_list[$items['uid']]['num'])?bcadd($user_list[$items['uid']]['num'],$items['num'],6):$items['num'];
			$user_list[$items['uid']]['yk']	=	isset($user_list[$items['uid']]['yk'])?bcadd($user_list[$items['uid']]['yk'],$items['yk'],6):$items['yk'];
		}

		//加载CFD产品model
		$this->load->model('cfd_model');		
		
		//加载CFD配置
		$this->load->config('cfd');

		if($cur_H != ($settlement_H+1)){
			$positionList = $this->cfd_model->getPosition(array('flag'=>0,'dtype'=>'ticket'),'all','id DESC');
		}
		if (!empty($positionList)) foreach($positionList as $position){
			if(!$this->cfdclass->setProduct($position['name'],'IB')){//设置产品
				continue;
			}
			if(!$this->cfdclass->setDenom($position['dtype'])){//设置计价方式
				continue;
			}

			$redisData		= $this->cfdClass->redisAllData();
			$lastest_price	= isset($redisData['trade']['LAST_PRICE']) ? $redisData['trade']['LAST_PRICE'] : 0;
			$position['total'] = bcadd($position['num'],$position['block_num']);
			$position_yk	= del0($this->cfdClass->countYK($lastest_price,$position['price'],$position['total'],$position['bs'],$position['commission'],$position['overnight']));

			if($position_yk < 0 && $position['dtype']=='ticket'){
				$position_yk	=	0;
			}
			
			$user_list[$position['uid']]['num']	=	isset($user_list[$position['uid']]['num'])?bcadd($position['total'],$user_list[$position['uid']]['num'],6):$position['total'];
			$user_list[$position['uid']]['yk']		=	isset($user_list[$position['uid']]['yk'])?bcadd($position_yk,$user_list[$position['uid']]['yk'],6):$position_yk;
		}
		
		$table	=	"rank_yk";
		$this->load->model('user_model');
		$datetime	=	date("Y-m-d H:i:s");
		if (!empty($user_list)) foreach($user_list as $uid=>$val){
			$user_info	=	$this->user_model->getUserByUid($uid);
			$add_arr	=	array(
				'uid'		=>	$uid,
				'nickname'	=>	$user_info['nickname'],	
				'num'		=>	$val['num'],
				'yk'		=>	$val['yk'],
				'date'		=>	$date,	
				'datetime'	=>	$datetime,	
			);
			$this->admin_db->replace($table,$add_arr);
		}
		echo "over";
	}
}
